#include "stdafx.h"  
#include <windows.h>   
#include <stdio.h>   
#include <rapi.h>    


#pragma comment(lib, "rapi.lib")   
#define BUFFSIZE 32*1024   

HRESULT TryRapiConnect(DWORD dwTimeOut)   
{   
	HRESULT            hr = E_FAIL;   
	RAPIINIT           riCopy;   
	BOOL          fInitialized = FALSE;   

	ZeroMemory(&riCopy, sizeof(riCopy));   
	riCopy.cbSize = sizeof(riCopy);   

	hr = CeRapiInitEx(&riCopy);   
	if (SUCCEEDED(hr))   
	{   
		DWORD dwRapiInit = 0;   
		fInitialized = TRUE;   

		dwRapiInit = WaitForSingleObject(   
			riCopy.heRapiInit,   
			dwTimeOut);   
		if (WAIT_OBJECT_0 == dwRapiInit)   
		{   
			//  heRapiInit signaled:   
			// set return error code to return value of RAPI Init function   
			hr = riCopy.hrRapiInit;     
		}   
		else if (WAIT_TIMEOUT == dwRapiInit)   
		{   
			// timed out: device is probably not connected   
			// or not responding   
			hr = HRESULT_FROM_WIN32(ERROR_TIMEOUT);   
		}   
		else   
		{   
			// WaitForSingleObject failed   
			hr = HRESULT_FROM_WIN32(GetLastError());   
		}   
	}   

	if (fInitialized && FAILED(hr))   
	{   
		CeRapiUninit();   
	}   
	return hr;   
}   

void copyCabFile(TCHAR *a_pFileName)   
{   
	HANDLE l_hPCFil = NULL;   
	HANDLE l_hDeviceFil = NULL;   
	PBYTE l_pBuffer = 0;   
	DWORD l_bytesRead = 0;   
	DWORD l_bytesWritten = 0;   
	BOOL l_bSuccess;   
	HRESULT hr = E_FAIL;   

	//Initialize Windows CE RAPI   
	if(ERROR_SUCCESS != TryRapiConnect(1000)){   
		printf("ERROR:the connection is not made!\r\n");   
		return;   
	}    

	// Allocate a buffer.   
	l_pBuffer = (PBYTE)LocalAlloc (LMEM_FIXED, BUFFSIZE);   

	l_hPCFil = CreateFile (a_pFileName,    
		GENERIC_READ,   
		FILE_SHARE_READ,   
		NULL,    
		OPEN_EXISTING,    
		FILE_ATTRIBUTE_NORMAL,   
		NULL);   
	//l_iFileSize = (int)GetFileSize (l_hPCFil, NULL);   
	//if (l_iFileSize  0) return 0;   
	if (l_hPCFil == INVALID_HANDLE_VALUE) {   
		return;   
	}   
	l_hDeviceFil = CeCreateFile(a_pFileName,   
		GENERIC_WRITE,   
		FILE_SHARE_READ,   
		NULL,   
		CREATE_ALWAYS,   
		FILE_ATTRIBUTE_NORMAL,   
		NULL);   

	if (l_hDeviceFil == INVALID_HANDLE_VALUE) {   
		return;   
	}   
	do {   
		// Read the file in PC.   
		l_bSuccess = ReadFile(l_hPCFil,   
			l_pBuffer,   
			BUFFSIZE,   
			&l_bytesRead,   
			NULL);   

		if (l_bSuccess){   
			l_bSuccess = CeWriteFile(l_hDeviceFil,   
				l_pBuffer,   
				l_bytesRead,   
				&l_bytesWritten,   
				NULL);   
		}   
	} while (l_bSuccess && (l_bytesRead));   

	l_bSuccess = CloseHandle(l_hPCFil);       
	l_bSuccess = CeCloseHandle(l_hDeviceFil);   
	if(l_pBuffer){   
		LocalFree(l_pBuffer);   
		l_pBuffer = NULL;   
	}   

	hr = CeRapiUninit();   
	printf("success\r\n");   
}   

void playSound()
{
	HRESULT hr = E_FAIL;
	IRAPIStream *pStream;
	DWORD	cbOut=NULL;
	BYTE*	pOut;

	//Initialize Windows CE RAPI   
	if(ERROR_SUCCESS != TryRapiConnect(1000)){   
		printf("ERROR:the connection is not made!\r\n");   
		return;   
	}    

	/*hr = HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND);
	hr = HRESULT_FROM_WIN32(ERROR_CALL_NOT_IMPLEMENTED);
	hr = HRESULT_FROM_WIN32(ERROR_EXCEPTION_IN_SERVICE);*/

	hr = CeRapiInvoke(L"\\\ResidentFlash\\PlaySound", L"fnPlaySound", NULL,
	NULL, &cbOut, NULL, NULL, 0);
	//	NULL, &cbOut, NULL, &pStream, 0);

	/*hr = CeRapiInvoke(L"PlaySound", L"CallMyFunction",
		0, NULL, &cbOut, &pOut, NULL, 0);
	if(cbOut)
		printf("Your device got %s KB of Physical Memory available", pOut);
	else
		printf("No memory available in the device");*/	//ok

	hr = CeRapiUninit();
}